package org.hhh.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hhh.basic.exception.BusinessException;
import org.hhh.basic.util.AjaxResult;
import org.hhh.basic.util.PageList;
import org.hhh.user.domain.User;
import org.hhh.user.dto.UserDto;
import org.hhh.user.query.UserQuery;
import org.hhh.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理接口数据对接json格式
 */

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口",description = "用户管理接口详细描述")
public class UserController {
    @Autowired
    private IUserService userService;

    //新增或修改
    @ApiOperation(value = "用户管理添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId()==null){
                userService.add(user);
            }else {
                userService.upadate(user);
            }
             return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙啊，请稍后");
        }
    }
    /**
     * 写接口三要素
     * 1.请求地址是什么
     * 2.请求的参数
     * 3.返回值是什么
     * @param id
     * @return
     */
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult removeId(@PathVariable("id") Long id){
        try {
            userService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙,殴打程序猿");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchTree(@RequestBody List<Long> ids){
        try {
            userService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙,殴打程序猿");
        }
    }

    //查询用户管理一条数据
    @GetMapping("/{id}")
    public User getById(@PathVariable("id") Long id){
        return userService.findById(id);
    }
    //查询全部数据
    @GetMapping("/list")
    public List<User> getAll(){
        return userService.findAll();
    }

    //分页+高级查询
    @PostMapping("/page")
    public PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.queryPage(query);
    }

    /**
     * 用户注册
     * @param userDto
     * @return
     */
    @PostMapping("/register/phone")
    public AjaxResult register(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙啊，请稍后");
        }
    }

}
